package com.sqpoll.android;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.viewpagerindicator.TabPageIndicator;

/**
 * The main activity with tabs of page indicator.
 * 
 * @author Younggue Bae
 */
public class MainActivity_1 extends SherlockFragmentActivity {

	public static final String TAG = "MainActivity";

	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_pageindicator);
		
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mTabsAdapter = new TabsAdapter(this);
		
    Bundle bundle = new Bundle();
    bundle.putString("title", getString(R.string.tab_home));
    mTabsAdapter.addTab(HomeFragment.class, bundle);
    
    bundle = new Bundle();
    bundle.putString("title", getString(R.string.tab_group_poll));
    mTabsAdapter.addTab(GroupListFragment.class, bundle);
    
    bundle = new Bundle();
    bundle.putString("title", getString(R.string.tab_hot_open_poll));
    mTabsAdapter.addTab(OpenPollListFragment.class, bundle);
    
    bundle = new Bundle();
    bundle.putString("title", getString(R.string.tab_new_open_poll));
    mTabsAdapter.addTab(OpenPollListFragment.class, bundle);
    
    /*
    bundle = new Bundle();
    bundle.putString("title", getString(R.string.tab_my_open_polls));
    mTabsAdapter.addTab(OpenPollFragment.class, bundle);
    */
    
    mViewPager.setAdapter(mTabsAdapter);
    
    TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
    indicator.setViewPager(mViewPager);
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.setDisplayShowTitleEnabled(true);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);

		switch (item.getItemId()) {
		case R.id.menu_new_poll:
			Toast.makeText(getBaseContext(), "You selected New poll", Toast.LENGTH_SHORT).show();
			break;

		case R.id.menu_settings:
			Toast.makeText(getBaseContext(), "You selected Settings", Toast.LENGTH_SHORT).show();
			break;
		}

		return true;
	}

	public static class TabsAdapter extends FragmentPagerAdapter {
		private final Context mContext;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
		}

		public void addTab(Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			mTabs.add(info);
			notifyDataSetChanged();
		}
		
		@SuppressLint("DefaultLocale")
		@Override
    public CharSequence getPageTitle(int position) {
			TabInfo info = mTabs.get(position);
      return info.args.getString("title").toUpperCase();
    }

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(), info.args);
		}
	}

}